package com.example.easybuy.service.impl;

import com.example.easybuy.mapper.EasybuyProductMapper;
import com.example.easybuy.pojo.EasybuyProduct;
import com.example.easybuy.pojo.Product;
import com.example.easybuy.service.EasybuyProductService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张子晨
 * @since 2021-03-12
 */
@Service
public class EasybuyProductServiceImpl implements EasybuyProductService {

    @Autowired
    private EasybuyProductMapper easybuyProductMapper;

    @Autowired
    private SolrClient solrClient;

    //通过楼层的ID获取限时楼层商品(升序查询6个)
    @Override
    public List<EasybuyProduct> getProductByParentId(Integer id) {

        return easybuyProductMapper.getProductByParentId(id);
    }

    //根据id获取商品信息
    @Override
    public EasybuyProduct getProductById(Integer id) {
        return easybuyProductMapper.getProductById(id);
    }

    @Override
    public Map<String, Object> getProductFromSolr(Map<String, Object> map) {
        SolrQuery query = new SolrQuery();
        //添加搜索条件
        if (map.get("name") != "") {
            query.setQuery("prod_name:" + map.get("name"));
        } else {
            query.setQuery("*:*");
        }

        if (map.get("categoryLevel1Id") != "") {
            query.addFilterQuery("prod_categoryLevel1Id:" + map.get("categoryLevel1Id"));
        } else if (map.get("categoryLevel2Id") != "") {
            query.addFilterQuery("prod_categoryLevel2Id:" + map.get("categoryLevel2Id"));
        } else if (map.get("categoryLevel3Id") != "") {
            query.addFilterQuery("prod_categoryLevel3Id:" + map.get("categoryLevel3Id"));
        }

        //添加过滤条件
        if (map.get("price") != null) {
            String p = (String) map.get("price");
            int index = p.indexOf("-");
            String start = p.substring(0, index);
            String end = p.substring(index + 1, p.length());
            query.addFilterQuery("prod_price:[" + start + " TO " + end + "]");
        }

        //添加排序条件
        String condition = (String) map.get("condition");
        if ("priceDown".equals(condition)) {
            query.addSort("prod_price", SolrQuery.ORDER.desc);
        } else if ("priceUp".equals(condition)) {
            query.addSort("prod_price", SolrQuery.ORDER.asc);
        } else if ("stockDown".equals(condition)) {
            query.addSort("prod_stock", SolrQuery.ORDER.desc);
        } else if ("stockUp".equals(condition)) {
            query.addSort("prod_stock", SolrQuery.ORDER.asc);
        } else {
            query.addSort("id", SolrQuery.ORDER.desc);
        }

        //设置高亮显示
        query.setHighlight(true);
        query.addHighlightField("prod_name");
        query.setHighlightSimplePre("<font color='red'>");
        query.setHighlightSimplePost("</font>");

        //分页
        query.setRows(4);
        query.setStart(query.getRows() * ((int) map.get("pageNum") - 1));

        Map<String, Object> resultMap = new HashMap<>();
        try {
            QueryResponse qr = solrClient.query(query);
            Map<String, Map<String, List<String>>> highlighting = qr.getHighlighting();
            List<Product> productList = qr.getBeans(Product.class);

            long numFound = qr.getResults().getNumFound();
            long pages = numFound % query.getRows() == 0 ? numFound / query.getRows() : numFound / query.getRows() + 1;
            //高亮显示的内容
            resultMap.put("highlighting", highlighting);
            //获取的商品
            resultMap.put("productList", productList);
            //符合要求商品总数量
            resultMap.put("numFound", numFound);
            //总页数
            resultMap.put("pages", pages);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public int reduceProduct(int stock, Integer productId) {
        return easybuyProductMapper.reduceProduct(stock, productId);
    }

}
